package pam.security;

import java.util.Iterator;
import java.util.List;

import pam.core.XMLObject;

/**
 * 角色类
 * 
 */
public class Role implements XMLObject {

	private String id;

	private String title;

	private List permissions;

	public Role() {
		super();
	}

	public Role(String id, String title) {
		super();
		this.id = id;
		this.title = title;
	}

	public Role(String id, String title, List permissions) {
		super();
		this.id = id;
		this.title = title;
		this.permissions = permissions;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public List getPermissions() {
		return permissions;
	}

	public void setPermissions(List permissions) {
		this.permissions = permissions;
	}

	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Role other = (Role) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	public String toXML() {

		StringBuffer sb = new StringBuffer();
		sb.append("<Role>");

		sb.append("<ID>");
		sb.append(getId() == null ? "" : getId());
		sb.append("</ID>");

		sb.append("<Title>");
		sb.append(getTitle() == null ? "" : getTitle());
		sb.append("</Title>");

		List list = getPermissions();
		if (list != null && list.size() > 0) {
			sb.append("<Permission-List>");

			Iterator iterator = list.iterator();

			while (iterator.hasNext()) {
				Permission permission = (Permission) iterator.next();
				if (permission == null) {
					continue;
				}
				sb.append(permission.toXML());
			}

			sb.append("</Permission-List>");

		}

		sb.append("</Role>");

		return sb.toString();

	}
}
